package richbar.com.richbar.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.common.base.BaseActivity;
import com.common.util.LogUtil;
import com.common.util.StringUtil;
import com.common.util.ToastUtil;
import com.hedgehog.ratingbar.RatingBar;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.okhttp.GlideUtil;
import com.okhttp.network.HttpUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import richbar.com.richbar.R;
import richbar.com.richbar.adapter.PhotoNewAdapter;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.help.TitleHelp;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.PhotoUtil;
import richbar.com.richbar.util.Util;
import richbar.com.richbar.view.NoScrollGridView;

/**
 * Created by Administrator on 2018/12/4.
 * 晒单评价
 */

public class EvaluateDetailAc extends BaseActivity {
    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.ratingbar)
    RatingBar ratingbar;
    @BindView(R.id.edit)
    EditText edit;
    @BindView(R.id.gridView)
    NoScrollGridView gridView;
    private String star;
    private PhotoNewAdapter adapter;
    private List<String> urls = new ArrayList<>();
    private List<LocalMedia> list = new ArrayList<>();
    private List<File> files;
    private LoadingDialog loadingDialog;

    @Override
    public int initLayout() {
        return R.layout.ac_evaluatedetail;
    }

    @Override
    public void initTitle() {
        new TitleHelp(this).setTitle("晒单评价").setRightTv("完成", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }

    private void submit() {
        String goods_id = getBundle().getString("goods_id");
        String oid = getBundle().getString("oid");
        String content = StringUtil.getStr(edit);
        if (content.length() < 30 || content.length() > 300) {
            ToastUtil.showToast(this, "字数限制30~300个!");
            return;
        }
        loadingDialog.show();
        String img="";
       // JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < urls.size(); i++) {
            //jsonArray.put(Util.imageToBase64(urls.get(i)));
            img=img+Util.imageToBase64(urls.get(i))+"@";
        }
        LogUtil.e("PPPPPPPPP", "goods_id:" + goods_id + "\n" + "oid:" + oid + "\n" + "star:" + star + "\n" +
                "content:" + content + "\n" + "uid:" + BaseApplication.uid + "\n" + "token:" + Util.md5() + "\n" + "img:" +img);
        NetWorkUtil.submitRate(img, goods_id, oid, star.substring(0, 1), content, new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                loadingDialog.dismiss();
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                loadingDialog.dismiss();
                ToastUtil.showToast(EvaluateDetailAc.this, msg);
            }
        });
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(this, 0);
        ratingbar.setOnRatingChangeListener(
                new RatingBar.OnRatingChangeListener() {
                    @Override
                    public void onRatingChange(float RatingCount) {
                        star = String.valueOf(RatingCount);
                        ratingbar.setStar(RatingCount);
                    }
                }
        );
        String goodsImg = getBundle().getString("goods_img");
        GlideUtil.loadImage(this, NetWorkUtil.URL + goodsImg, iv);
    }

    @Override
    public void initData() {
        adapter = new PhotoNewAdapter(this, urls, list, new PhotoNewAdapter.OnAddPicClickListener() {
            @Override
            public void onAddPicClick() {
                PhotoUtil.openPhotoMuti(EvaluateDetailAc.this, 6, list);
            }
        });
        gridView.setAdapter(adapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureConfig.CHOOSE_REQUEST) {
            list = PictureSelector.obtainMultipleResult(data);
            if (null == list || list.size() == 0) {
                return;
            }
            urls = new ArrayList<>();
            files = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                urls.add(list.get(i).getCompressPath());
                files.add(new File(list.get(i).getCompressPath()));
            }
            adapter.setData(list);
            adapter.setUrls(urls);
            adapter.notifyDataSetChanged();
        }
    }
}
